﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Allocatin_AlloFlag : ATPages
{
    ZSql zs = new ZSql();
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        txtKdate.Attributes.Add("onclick", "SelectDate('txtKdate')");
        txtJdate.Attributes.Add("onclick", "SelectDate('txtJdate')");
        if (!IsPostBack)
        {
            Hidid.Value = Request.Cookies["user"].Values["id"];
            HidWareid.Value = getWareId();
            lblPath.Text = f.getModuleidAdd(Request["moduid"]);
            initData();
         
        }
        f.checkLevels(Request.Cookies["user"].Values["roleid"], Request["moduid"], 13, GvData, btnDel);
    }
    private string getWareId()
    {
        ZSql zs = new ZSql();
        string sql = "select WarehouseId from UserInfo where userid=" + Hidid.Value;
        zs.Open(sql);
        zs.Close();
        return zs.m_table.Rows[0]["WarehouseId"].ToString();
    }

    private void initData()
    {
        string sql = "select *,convert(char(10),alloDate,120) as adate,allowarehouse=(select warehousename from CS_Warehouse where warehouseid=a.alloware),recewarehouse=(select warehousename from CS_Warehouse where warehouseid=a.receware ) from CS_alloction a   ";
        //zs.Open(sql);

        if (HidWareid.Value!="0")
        {
            sql += " where a.alloware="+HidWareid.Value;
        }
        sql += " order by a.adate desc";
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;
        //GvData.DataSource = zs.m_table;
        //GvData.DataBind();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {

        string sql = "select *,convert(char(10),alloDate,120) as adate,allowarehouse=(select warehousename from CS_Warehouse where warehouseid=a.alloware),recewarehouse=(select warehousename from CS_Warehouse where warehouseid=a.receware ) from CS_alloction a where 1=1";

        if (!string.IsNullOrEmpty(txtCode.Text))
        {
            sql += " and a.Comodid='" + Common.FormatParameter(txtCode.Text) + "'";
        }
        if (ddlstate.SelectedIndex!=0)
        {
            sql += " and a.AuditState='" +ddlstate.SelectedItem.Text + "'";
        }
        if (!string.IsNullOrEmpty(txtdh.Text))
        {
            sql += " and alloCode='" + Common.FormatParameter(txtdh.Text) + "'";
        }
        if (!string.IsNullOrEmpty(txtKdate.Text) && !string.IsNullOrEmpty(txtJdate.Text.Trim()))
        {
            sql += " and a.alloDate >='" + Common.FormatParameter(txtKdate.Text) + " 00:00:00' and a.alloDate<='" + Common.FormatParameter(txtJdate.Text) + " 23:59:59'";
        }
        if (HidWareid.Value!="0")
        {
            sql += " and a.alloware=" + HidWareid.Value;
        }
        sql += "  order by a.adate desc ";
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        int n = 0;
        if (this.GvData.Rows.Count > 0)
        {
            for (int i = 0; i < GvData.Rows.Count; i++)
            {
                CheckBox ck = GvData.Rows[i].Cells[13].FindControl("ckFlag") as CheckBox;
                if (ck.Checked)
                {

                    zs.Open("delete CS_alloction  where alloCode='"+this.GvData.Rows[i].Cells[0].Text+"'" );
                    Response.Write("<script>window.location='AlloFlag.aspx?moduid=" + Request["moduid"] + "';</script>");
                }
                else
                {
                    n++;
                }

                if (n == this.GvData.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }


            }
        }
    }
    protected void linkbtnFlag_Click(object sender, EventArgs e)
    {
        string code = (sender as LinkButton).CommandArgument;
        Response.Redirect("Alloction.aspx?id="+code+"&action=flag&moduid="+Request["moduid"]);
    }
    protected void linkbtnPrint_Click(object sender, EventArgs e)
    {
        string id = (sender as LinkButton).CommandArgument;
        ClientScript.RegisterStartupScript(typeof(Page), "aa", "PrintData('" + id + "')", true);
    }
    protected void GvData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        
            f.checkLevels(Request.Cookies["user"].Values["roleid"], Request["moduid"], 13, GvData, btnDel);
        
    }
}
